package cn.bjxy.shishaolong.request_answer.service;

import cn.bjxy.shishaolong.request_answer.entity.Answer;
import cn.bjxy.shishaolong.request_answer.mapper.AnswerMapper;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.TreeMap;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-16
 */
@Service
@Slf4j
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public IPage<Answer> queryByCondition(IPage<Answer> page, Answer answer) {
        List<Answer> answerList = answerMapper.queryByCondition(page, answer);
        if (answerList != null && answerList.size() > 0
                && (answer.getAnswerOwn() == null)) // 如果是查询的个人回答列表不用结构化
            generatorTreeAnswerList(answerList);
        return page.setRecords(answerList);
    }

    @Override
    public List<Answer> queryByCondition(Answer answer) {
        List<Answer> answerList = answerMapper.queryByCondition(answer);
        if (answerList != null && answerList.size() > 0
                && (answer.getAnswerOwn() == null))
            generatorTreeAnswerList(answerList);
        return answerList;
    }

    /**
     * 将回答列表生成树状结构
     *
     * @param answerList
     */
    private void generatorTreeAnswerList(List<Answer> answerList) {
        TreeMap<Integer, Answer> treeMap = new TreeMap<>();
        // 结构化回答列表
        for (Answer answerTemp : answerList) {
            if (answerTemp.getPid() != null && answerTemp.getPid() == -1) { // 存储根节点
                treeMap.put(answerTemp.getTid(), answerTemp); // 创建根节点
            } else { // pid != -1 非根节点
                Answer treeAnswer = treeMap.get(answerTemp.getPid()); // 取出根节点
                if (treeAnswer != null) { // 存在根节点
                    List<Answer> attachAnswerList = treeAnswer.getAttachAnswer();
                    if (attachAnswerList != null && attachAnswerList.size() > 0) { // 追问回答中已存在元素
                        attachAnswerList.add(answerTemp); // 将当前元素添加到追问列表
                    } else { // 根节点还没有创建追问列表
                        ArrayList<Answer> tempAttachAnswer = new ArrayList<>(); //创建追问列表
                        tempAttachAnswer.add(answerTemp); // 向追问列表中添加元素
                        treeAnswer.setAttachAnswer(tempAttachAnswer); // 将追问列表添加到根节点中
                    }
                } else { // 不存在此根节点 报错
                    throw new OperatorFailureException(-1, answerTemp.toString() + "根节点未找到");
                }
            }
        }
        // 重置回答列表
        answerList.clear(); //清空回答列表
        for (Integer key : treeMap.keySet()) { // 将treeMap中的每一项依次添加到回答列表中
            Answer treeAnswer = treeMap.get(key);
            if (!Objects.isNull(treeAnswer)) {
                answerList.add(treeAnswer);
            }
        }
    }
}
